package cn.hxzy.zy.office.util;


import cn.hxzy.zy.office.entity.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author makejava
 * @since 2020-08-06 11:10:04
 */
public class FirstInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        String requestUrl = request.getRequestURI();
        Object loginUser = request.getSession().getAttribute("loginUser");
        System.out.println(loginUser);
        User user = (User) loginUser;
        if (user == null) {
            response.sendRedirect("logout");
        } else {
            if (user.getRoleId() == 0) {
                return true;
            } else {
                if (requestUrl.endsWith(UriEndWith.userIndex.toString()) || requestUrl.endsWith(UriEndWith.searchUserSchedule.toString()) || requestUrl.endsWith(UriEndWith.saveUserSchedule.toString()) || requestUrl.endsWith(UriEndWith.deleteUserSchedule.toString()) || requestUrl.endsWith(UriEndWith.updateUserSchedule.toString()) || requestUrl.endsWith(UriEndWith.createUserSchedule.toString()) || requestUrl.endsWith(UriEndWith.editUserSchedule.toString()) || requestUrl.endsWith(UriEndWith.searchMyDoc.toString()) || requestUrl.endsWith(UriEndWith.searchNewsInfo.toString())) {
                    return true;
                } else {
                    response.sendRedirect("userIndex");
                }
            }
        }
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
